**************************
***	RUN USING STATA 16 ***
**************************

* Purpose: rename variables with universal reference (items exist on multiple forms)
* Last Updated: 06 May 2021
	
*--------------------------------------------------------------------------------------------------------------------------------
* objective items
*--------------------------------------------------------------------------------------------------------------------------------

	//loop through versions, prepare response data
	forvalues k = 1/30 {
		use "$output\mat_o_clean.dta", clear
		
		*retain only relevant form and vars
		keep if version==`k'
		keep pid version r1-r32
		order r*, seq
		order pid version
		*convert all to string, so to renvars from observation [1] later
		tostring r1-r32 pid version, replace force

	tempfile ver
	save `ver'	
	
	//lookup and setup unique variable names
	import excel "$input\mat key_domain.xlsx", sheet("location") firstrow clear

		*do for current form/version
		keep if form==`k'
		drop source
		gen un = (src_yr-2010)*100 + src_loc
		keep if src_typ=="obj"
		replace src_typ = "o_" if src_typ=="obj"
		egen r = concat(src_typ un)
		drop src_yr src_loc src_typ un
		*reshape data to retain one observation with unique variable names
		reshape wide r, i(form) j(location)
		drop form
		gen version = "version"
		gen pid = "pid"
		
	//combine responses with universal references
	append using `ver'
	
		*rename all vars using universal reference
		renvars, map(strtoname(@[1]))
		drop if pid=="pid"
		order version, first
		order pid, last
		*destring all
		destring version - pid, replace
		
	tempfile ver`k'
	save `ver`k''
	
	}
	
	//combine all forms/versions
	use `ver1'
		
		forvalues l = 2/30 {
		append using `ver`l''
		}
		
		*remove all variable labels
		foreach var of varlist _all {
		label var `var' ""
		}
	
	ren version booklet
	
	order booklet pid, first
	
	//save
	tempfile objective
	save `objective'
	
*--------------------------------------------------------------------------------------------------------------------------------
* subjective items
*--------------------------------------------------------------------------------------------------------------------------------

	//loop through versions, prepare response data
	forvalues k = 1/30 {
		use "$output\mat_s_clean.dta", clear

		*retain only relevant form and vars
		keep if version==`k'
		keep pid version v33 - v41
			forvalues i =33/41 {
			ren v`i' r`i'
			}
		order pid version
		*convert all to string, so to renvars from observation [1] later
		tostring pid-r41, replace force

	tempfile ver
	save `ver'	
		
	//lookup and setup unique variable names
	import excel "$input\mat key_domain.xlsx", sheet("location") firstrow clear

		*do for current form/version
		keep if form==`k'
		drop source
		gen un = (src_yr-2010)*100 + src_loc
		keep if src_typ=="sub"
		replace src_typ = "s_" if src_typ=="sub"
		egen r = concat(src_typ un)
		drop src_yr src_loc src_typ un
		*reshape data to retain one observation with unique variable names
		reshape wide r, i(form) j(location)
		drop form
		gen version = "version"
		gen pid = "pid"
		
	//combine responses with universal references
	append using `ver'
	
		*rename all vars using universal reference
		renvars, map(strtoname(@[1]))
		drop if pid=="pid"
		order version, first
		order pid, last
		*destring all
		destring version - pid, replace
		
	tempfile ver`k'
	save `ver`k''
	
	}
	
	//combine all forms/versions
	use `ver1'
		
		forvalues l = 2/30 {
		append using `ver`l''
		}
		
		*remove all variable labels
		foreach var of varlist _all {
		label var `var' ""
		}
	
	ren version booklet

	order booklet pid, first
	
	merge 1:1 booklet pid using `objective'
	
	ren _merge set
	lab def set 1 "sub only" 2 "obj only" 3 "both"
	lab values set set
	
	//save
	save "$output\mat_unique.dta", replace	
		
	
	
	
	
	
	
	
	
	